# # coding=utf-8
from auto_interface.base.common_def import Base_com
import xlrd
from xlutils.copy import copy
import time
from auto_interface.parameter_case.file_path import File_path
from auto_interface.parameter_case.login_parameter import Login_par
# from auto_interface.html_file import Html

class Excel_in():
    # 打开文件
    data = xlrd.open_workbook(File_path.excel_path)
    new_1 = copy(data)
    start_time = time.strftime('%Y%m%d%H%M%S')
    print("开始时间：" + str(start_time))
    counter = 0
    counter_false = 0

    # 查看工作表
    # table = ""
    data.sheet_names()
    print("全部sheet页如下：\n" + str(data.sheet_names()))
    for s in data.sheet_names():
        table = data.sheet_by_name(s)
        # 通过文件名获得工作表,获取工作表1
        # 打印data.sheet_names()可发现，返回的值为一个列表，通过对列表索引操作获得工作表1
        # table = data.sheet_by_index(0)

        # 获取行数和列数
        # 行数：table.nrows
        # 列数：table.ncols
        print("总行数：" + str(table.nrows-1))
        print("总列数：" + str(table.ncols))

        # 获取整行的值 和整列的值，返回的结果为数组
        # 整行值：table.row_values(start,end)
        # 整列值：table.col_values(start,end)
        # 参数 start 为从第几个开始打印，
        # end为打印到那个位置结束，默认为none
        # print("整行值：" + str(table.row_values(0)))
        # print("整列值：" + str(table.col_values(1)))

        for i in range(1, table.nrows):
            print("-----------------------sheet页:%s-第%s行---------------------：" % (s, str(i)))
            case_no = table.cell(i, 0).value
            # print("用例序号：" + table.cell(i, 0).value)
            case_path = table.cell(i, 2).value
            # print("路径：" + table.cell(i, 2).value)
            case_parm = table.cell(i, 3).value
            if case_parm is "":
                case_parm = {"size":10,"page":1}
            # print("传参：" + table.cell(i, 3).value)
            case_assrt = table.cell(i, 4).value
            # print("预期结果：" + table.cell(i, 4).value)
            case_0 = table.cell(i, 8).value
            # 测试轮次

            request_data = Base_com().post_requests(login_path=case_path, data=case_parm, ass_par=case_assrt)
            counter += 1

            if case_0 is '':
                case_0 = 1
            else:
                case_0 += 1

            # new_1 = copy(data)
            wr_sheet = new_1.get_sheet(s)

            try:
                wr_sheet.write(i, 1, str(Login_par.username))  # 所用账号
                wr_sheet.write(i, 5, str(request_data[1]))  #实际结果
                wr_sheet.write(i, 6, str(request_data[0]))  #是否通过
                if request_data[0] is False:
                    # print(request_data[2])
                    counter_false += 1
                    wr_sheet.write(i, 7, str(request_data[2]))  #错误类型
                    if 'token错误' == str(request_data[2]):
                        new_1.save(File_path.set_path)  # 另存文件
                        new_1.save(File_path.excel_path)  # 更新测试用例文件
                        break
                wr_sheet.write(i, 8, int(case_0))   #执行轮次
            except Exception as E:
                print("写入失败：", E)

    end_time = time.strftime('%Y%m%d%H%M%S')
    print("结束时间：" + str(end_time))
    new_1.save(File_path.set_path)  # 另存文件
    new_1.save(File_path.excel_path)  # 更新测试用例文件
    print("共执行用例：%s条；\n失败：%s条" % (counter, counter_false))

if __name__ == '__main__':
    Excel_in()

# import xlrd
#
# def read_xlrd(excelFile):
#     data = xlrd.open_workbook(excelFile)
#     table = data.sheet_by_index(0)
#
#     for rowNum in range(table.nrows):
#         rowVale = table.row_values(rowNum)
#         for colNum in range(table.ncols):
#             if rowNum > 0 and colNum == 0:
#                 print(int(rowVale[0]))
#             else:
#                 print(rowVale[colNum])
#         print("---------------")
#
#     # if判断是将 id 进行格式化
#     # print("未格式化Id的数据：")
#     # print(table.cell(1, 0))
#     # 结果：number:1001.0
#
#
# if __name__ == '__main__':
#     excelFile = 'file/demo.xlsx'
#     read_xlrd(excelFile=excelFile)


# import xlrd
# from xlutils.copy import copy

# data = xlrd.open_workbook('D:\work\工作安排\进出区提发货凭证\项目文件\接口用例模板.xlsx')
# readOnlySheet = data.sheet_by_index(0)  # 通过sheet_by_index()获取的sheet没有write()方法
# writableWorkbook = copy(data)  # 利用xlutils.copy函数，将xlrd.Book转为xlwt.Workbook
# writableSheet = writableWorkbook.get_sheet(0)  # 通过get_sheet()获取的sheet有write()方法
# writableSheet.write(3, 4, 'insertData')  # 写入3,3这个单元格
#
# writableWorkbook.save('D:\work\工作安排\进出区提发货凭证\项目文件\接口用例模板.xlsx')  # 保存文件